package edu.washington.cs.workflow.recommender;

import java.util.List;
import java.util.Set;
import junit.framework.TestCase;

public class TestParsingTraceFileJabRef extends TestCase {

	//Click menu item: null, Search,
	public void testActionsJabRef() {
		String file = "./tests/files/jabref_all_actions_new_version_24m.txt";
		EventTraceProcessor processor = new EventTraceProcessor(file);
		//List<UIActionHandler> handlers = processor.getAllActionHandlers();
		List<String> uniqueHandler = processor.getAllUniqueActionHandlerDescs();
		System.out.println(uniqueHandler.size());
		
		for(String handler : uniqueHandler) {
			System.out.println(handler);
		}
		
		System.out.println("Analyzing the invoking methods...");
		
		String startSearch = "net.sf.jabref.SearchManager2.startSearch()V";
		List<String> actions = processor.getTriggeringActionDescs(startSearch);
		System.out.println(actions);
		assertEquals(1, actions.size());
		
		String importIntoDb = "net.sf.jabref.imports.ImportFormats$1ImportAction.actionPerformed(Ljava/awt/event/ActionEvent;)V";
		actions = processor.getTriggeringActionDescs(importIntoDb);
		System.out.println(actions);
		assertEquals(2, actions.size());
		
		String exportAction = "net.sf.jabref.export.ExportFormats$1ExportAction.actionPerformed(Ljava/awt/event/ActionEvent;)V";
		actions = processor.getTriggeringActionDescs(exportAction);
		System.out.println(actions);
		assertEquals(2, actions.size());
	}
	
	//the trace file produced by the old version
	//go to the experiments/jabref2.0, and use the following command:
	//  java -cp ./jabref-2.0-instr.jar;./*;../../bin;../../libs/simmetrics_jar.jar net.sf.jabref.JabRef
	
	//TODO note that first check whether the handler method has been invoked or not
	//if not, just skip it; otherwise, look at each invoked method
	public void testJabRef_old__search_demo() {
		String traceFile = "./tests/files/jabref_all_actions_new_version_24m.txt";
		EventTraceProcessor traceProcessor = new EventTraceProcessor(traceFile);
		
		String file = "./tests/demo-traces/jabref-search-old-version.txt";
		EventTraceProcessor processor = new EventTraceProcessor(file);
		
		this.showInvokedHandlerAndActions(processor, traceProcessor);
	}
	
	public void testJabRef_old_import_demo() {
		String traceFile = "./tests/files/jabref_all_actions_new_version_24m.txt";
		EventTraceProcessor traceProcessor = new EventTraceProcessor(traceFile);
		
		String file = "./tests/demo-traces/jabref-import-old-version.txt";
		EventTraceProcessor processor = new EventTraceProcessor(file);
		
		this.showInvokedHandlerAndActions(processor, traceProcessor);
	}
	
	public void testJabRef_old_import_and_append_demo() {
		String traceFile = "./tests/files/jabref_all_actions_new_version_24m.txt";
		EventTraceProcessor traceProcessor = new EventTraceProcessor(traceFile);
		
		String file = "./tests/demo-traces/jabref-import-and-append-old-version.txt";
		EventTraceProcessor processor = new EventTraceProcessor(file);
		
		this.showInvokedHandlerAndActions(processor, traceProcessor);
	}
	
	//This is hard coded in the end of this file
	public void testJabRef_old_export_demo() {
		String traceFile = "./tests/files/jabref_all_actions_new_version_24m.txt";
		EventTraceProcessor traceProcessor = new EventTraceProcessor(traceFile);
		
		String file = "./tests/demo-traces/jabref-export-old-version.txt";
		EventTraceProcessor processor = new EventTraceProcessor(file);
		
		this.showInvokedHandlerAndActions(processor, traceProcessor);
	}
	
	private void showInvokedHandlerAndActions(EventTraceProcessor oldProcessor, EventTraceProcessor newProcessor) {
		List<String> handlerDescs = oldProcessor.getAllUniqueActionHandlerDescs();
		for(String desc : handlerDescs) {
			System.out.println("-- handler : " + desc);
			//FIXME hard code it
			if(desc.equals("net.sf.jabref.JabRefFrame$7.actionPerformed(Ljava/awt/event/ActionEvent;)V")) {
				desc = "net.sf.jabref.export.ExportFormats$1ExportAction.actionPerformed(Ljava/awt/event/ActionEvent;)V";
					//"net.sf.jabref.export.ExportFormats$1ExportAction.actionPerformed(Ljava/awt/event/ActionEvent;)V";
			}
			System.out.println("      actions: " + newProcessor.getTriggeringActionDescs(desc));
			System.out.println("its invoked method: ");
			List<String> methods = oldProcessor.getInvokedMethodsByHandler(desc);
			for(String m : methods) {
//				System.out.println("   " + m);
				//then query the 
				
				List<String> descs = newProcessor.getTriggeringActionDescs(m);
//				System.out.println("      actions: " + descs);
				
			}
		}
	}
	
}
